package com.lib.common.dialog_fragment;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import com.lib.utils.XLog;

@SuppressLint("ValidFragment")
class BaseDialogFragment extends DialogFragment {

    View mView;
    private boolean mIsNeedFullScreen;
    private int mGravity = -1;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return mView;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog == null) {
            return;
        }
        dialog.setCanceledOnTouchOutside(false);
        FragmentActivity activity = getActivity();
        if (activity == null) {
            return;
        }
        Window dialogWindow = dialog.getWindow();
        if (dialogWindow == null) {
            return;
        }
        if (mIsNeedFullScreen) {
            View decorView = dialogWindow.getDecorView();
            dialogWindow.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

            decorView.setPadding(0, 0, 0, 0);
            decorView.setBackgroundColor(Color.WHITE);
            dialogWindow.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        } else {
            dialogWindow.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            if (mGravity != -1) {
                WindowManager.LayoutParams wlp = dialogWindow.getAttributes();
                wlp.gravity = mGravity;
                dialogWindow.setAttributes(wlp);
            }
        }
    }

    public BaseDialogFragment show(FragmentActivity activity) {
        return show(activity, false, Gravity.CENTER);
    }

    public BaseDialogFragment show(FragmentActivity activity, int gravity) {
        return show(activity, false, gravity);
    }

    public BaseDialogFragment show(FragmentActivity activity, boolean isNeedFullScreen) {
        return show(activity, isNeedFullScreen, Gravity.CENTER);
    }

    private BaseDialogFragment show(FragmentActivity activity, boolean isNeedFullScreen, int gravity) {
        if (!isNeedFullScreen) {
            mGravity = gravity;
        }
        mIsNeedFullScreen = isNeedFullScreen;
        FragmentManager manager = activity.getSupportFragmentManager();

        String name = getClass().getName();
        String tag = name + hashCode();
        // Fragment fragment = manager.findFragmentByTag(tag);
        // if (fragment != null) {
        // 	manager.beginTransaction().remove(fragment).commitAllowingStateLoss();
        // }

        try {
            show(manager, tag);
        } catch (Throwable e) {
            XLog.w(Log.getStackTraceString(e));
        }
        return this;
    }

    @Override
    public void dismiss() {
        super.dismissAllowingStateLoss();
    }
}